<?php
	require('../database/bd_connect.php');
		
	if (isset($_GET['opt'])){	
		$cpe = $_GET['opt'] ;
	
	$ultimo1minuto;
	$ultimo5minuto;
	$ultimo15minuto;
	$ultimo40minuto;
	
	//teste
	//$sql = "SELECT abs(total_noise) AS noise FROM noise_data WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 4 HOUR LIMIT 13";
for($i=1;$i<=13;$i++){
	//uma para último dia
	$sql = "SELECT avg(abs(average)) AS noise FROM noise_hour WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 1 DAY and channel=".$i;
	
	$result = $conn->query($sql);
	$num_rows = mysqli_num_rows($result);

	while ($row = mysqli_fetch_array($result)) {
		$ultimo1minuto[] = $row['noise'];				
	}
	//var_dump($ultimo1minuto);

	//uma para últimos 3 dias
	$sql = "SELECT avg(abs(average)) AS noise FROM noise_hour WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 3 DAY and channel=".$i;
	
	$result = $conn->query($sql);
	$num_rows = mysqli_num_rows($result);

	while ($row = mysqli_fetch_array($result)) {
		$ultimo5minuto[] = $row['noise'];				
	}
	//var_dump($ultimo5minuto);

	//uma para últimos 5 dias
	$sql = "SELECT avg(abs(average)) AS noise FROM noise_hour WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 5 DAY and channel=".$i;
	
	$result = $conn->query($sql);
	$num_rows = mysqli_num_rows($result);

	while ($row = mysqli_fetch_array($result)) {
		$ultimo15minuto[] = $row['noise'];				
	}
	//var_dump($ultimo15minuto);

	//uma para últimos 7 dias
	$sql = "SELECT avg(abs(average)) AS noise FROM noise_hour WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 7 DAY and channel=".$i;

	//ultimos 7 dias intervalos 12 horas
	//$sql = "SELECT abs(average) AS noise FROM noise_hour WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 7 DAY AND (hour(time)=0 OR hour(time)=12) GROUP BY channel";
	
	$result = $conn->query($sql);
	$num_rows = mysqli_num_rows($result);

	while ($row = mysqli_fetch_array($result)) {
		$ultimo40minuto[] = $row['noise'];				
	}
}
	$output = array($ultimo1minuto,$ultimo5minuto,$ultimo15minuto,$ultimo40minuto);

	echo json_encode($output);
	//echo "<br \>---fim" ;
}
else {
	echo "erro" ;
}
mysqli_close($conn);
?>
